package com.zjsru.plan2024.normal;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: cookLee
 * @Date: 2024-01-17
 * 汇总区间
 */
public class SummaryRanges {

    /**
     * 主
     * \
     * 输入：nums = [0,1,2,4,5,7]
     * 输出：["0->2","4->5","7"]
     * 解释：区间范围是：
     * [0,2] --> "0->2"
     * [4,5] --> "4->5"
     * [7,7] --> "7"
     * \
     * @param args args
     */
    public static void main(String[] args) {
        SummaryRanges summaryRanges = new SummaryRanges();
        int[] nums = new int[]{0,1,2,4,5,7};
        System.out.println(summaryRanges.summaryRanges(nums));
    }

    /**
     * 摘要范围
     *
     * @param nums nums
     * @return {@link List}<{@link String}>
     */
    public List<String> summaryRanges(int[] nums) {
        List<String> res = new ArrayList<>();
        int len = nums.length;
        int i = 0;
        while (i < len) {
            int low = i;
            i++;
            //校验数字是否是按照1连续增加
            while (i < len && nums[i] == nums[i - 1] + 1) {
                i++;
            }
            //最高位
            int high = i - 1;
            //组合所需格式,如果存在low==high则直接放入原数字
            StringBuilder sb = new StringBuilder(String.valueOf(nums[low]));
            if (low < high) {
                sb.append("->");
                sb.append(String.valueOf(nums[high]));
            }
            res.add(sb.toString());
        }
        return res;
    }

}
